local fengtian = fk.CreateSkill {
    name = "jlspgod__fengtian",
}

-- 回合开始时弃牌
fengtian:addEffect(fk.TurnStart, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(self.name) and target ~= player
    end,
    on_cost = function(self, event, target, player, data)
        if player:getHandcardNum() > 0 then
            local cards = player.room:askForCard(player, 1, 1, true, self.name, true, ".", "#jlspgod__fengtian_cost")
            if #cards > 0 then
                event:setCostData(self, {discarded_card = cards[1]})
                return true
            end
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local cost_data = event:getCostData(self)
        local cid = cost_data.discarded_card
        local card=Fk:getCardById(cid)
        room:throwCard(card,fengtian.name, player,player)
        if card.trueName == "slash" then
            room:setPlayerMark(target, "@@jlspgod__fengtian_invalid-turn", 1)
            
        end
        room:setPlayerMark(player, "@@jlspgod__fengtian_power-turn", target.id)--玩家获得封天标记
       
    end,
})

--使用牌后
fengtian:addEffect(fk.CardUseFinished, {
    anim_type = "drawcard",
    can_trigger = function(self, event, target, player, data)
      return  player:hasSkill(fengtian.name) and player:getMark("@@jlspgod__fengtian_power-turn")
      and  player:getMark("@@jlspgod__fengtian_power-turn") == target.id and
        data.extra_data and data.extra_data.fengtian
    end,
    on_cost = function(self, event, target, player, data)
        return true
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:useVirtualCard("slash", nil, player, target, fengtian.name, true)
    end,
  
    can_refresh = function (self, event, target, player, data)
      return player:hasSkill(fengtian.name) and player:getMark("@@jlspgod__fengtian_power-turn")
      and  player:getMark("@@jlspgod__fengtian_power-turn") == target.id and
        not table.contains(player:getTableMark(fengtian.name.."turn"), data.card.trueName)
    end,
    on_refresh = function (self, event, target, player, data)
      player.room:addTableMark(player, fengtian.name.."turn", data.card.trueName)
      data.extra_data = data.extra_data or {}
      data.extra_data.fengtian = true
    end,
})

--首次摸牌后
fengtian:addEffect(fk.AfterDrawNCards, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(fengtian.name) and player:getMark("@@jlspgod__fengtian_power-turn")
        and  player:getMark("@@jlspgod__fengtian_power-turn") == target.id
        and target:getMark("jlspgod__fengtian_draw-turn") == 0
    end,
    on_cost = function(self, event, target, player, data)
        return true
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:setPlayerMark(target, "jlspgod__fengtian_draw-turn", 1)
        room:useVirtualCard("slash", nil, player, target, fengtian.name, true)

    end,
})

--首次弃牌
fengtian:addEffect(fk.AfterCardsMove, {
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(fengtian.name) and player:getMark("@@jlspgod__fengtian_power-turn")  then
            local room=player.room
            local tar=room:getPlayerById(player:getMark("@@jlspgod__fengtian_power-turn"))
            for _, move in ipairs(data) do
              if move.from == tar and move.moveReason == fk.ReasonDiscard then
                for _, info in ipairs(move.moveInfo) do
                  if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                    return tar:getMark("jlspgod__fengtian_discard-turn") == 0
                  end
                end
              end
            end
        end
    end,
    on_cost = function(self, event, target, player, data)
        return true
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local tar=room:getPlayerById(player:getMark("@@jlspgod__fengtian_power-turn"))
        room:setPlayerMark(tar, "jlspgod__fengtian_discard-turn", 1)
        room:useVirtualCard("slash", nil, player, tar, fengtian.name, true)
    end,
})

-- 造成伤害后清除效果
fengtian:addEffect(fk.Damage, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(self.name) and player:getMark("@@jlspgod__fengtian_power-turn") and 
               data.to == player and data.from and player:getMark("@@jlspgod__fengtian_power-turn") == data.from.id
    end,
    on_cost = function(self, event, target, player, data)
        return true
    end,
    on_use = function(self, event, target, player, data)
        -- 清除所有相关标记
        player.room:setPlayerMark(target,"@@jlspgod__fengtian_invalid-turn", 0)
        player.room:setPlayerMark(player, "@@jlspgod__fengtian_power-turn", 0)
    end,
})

--技能失效
fengtian:addEffect("invalidity", {
    invalidity_func = function(self, from, skill)
      return from:getMark("@@jlspgod__fengtian_invalid-turn") > 0 and skill:isPlayerSkill(from)
    end
  })

Fk:loadTranslationTable{
    ["jlspgod__fengtian"] = "封天",
    [":jlspgod__fengtian"] = [[其他角色的回合开始时，你可以弃置一张牌，
    若如此做，该角色于本回合内首次摸牌、弃牌或使用每种牌名的牌后，你视为对其使用【杀】，
    若你弃置的牌为【杀】，你令其所有技能无效，上述效果持续至本回合结束或其对你造成伤害。]],
    ["#jlspgod__fengtian_cost"] = "封天：你可以弃置一张牌，若如此做，该角色于本回合内首次摸牌、弃牌或使用每种牌名的牌后，你视为对其使用【杀】",
    ["@@jlspgod__fengtian_invalid-turn"] = "封天失效",
    ["@@jlspgod__fengtian_power-turn"] = "封天",
    ["$jlspgod__fengtian1"]="天道如炼。神威如狱。此方天地，敕令生死！",
    ["$jlspgod__fengtian2"]="汉运未逆。安命已尽。封天绝地，誓戮奸邪！",

}

return fengtian 